using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz;

using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Stock
{
	/// <summary>
	/// Summary description for InventorySearch.
	/// </summary>
	public partial class InventorySearch : PageBase
	{
		protected EntLibEC.View.Cmn.Stock ucStock;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			DataGrid1.PageSize = AppConst.PageSize;
			DataGrid1.PagerStyle.PageButtonCount = AppConst.PageButtonCount;

			DataGrid2.PageSize = AppConst.PageSize;
			DataGrid2.PagerStyle.PageButtonCount = AppConst.PageButtonCount;

			if ( !Page.IsPostBack)
			{
				ucStock.BindStock(true);
			}

		}

		private void BindGrid()
		{
//			Hashtable ht = new Hashtable(7);
//
//			if ( txtAdjustID.Text.Trim() != "" )
//				ht.Add("AdjustID", txtAdjustID.Text.Trim());
//			if ( ucProduct.ProductSysNo != AppConst.IntNull )
//				ht.Add("ProductSysNo", ucProduct.ProductSysNo );
//			if ( ucStock.StockSysNo != AppConst.IntNull )
//				ht.Add("StockSysNo", ucStock.StockSysNo);
//			if (txtDateFrom.Text.Trim() != "")
//				ht.Add("DateFrom", txtDateFrom.Text.Trim());
//			if ( txtDateTo.Text.Trim() != "")
//				ht.Add("DateTo", txtDateTo.Text.Trim());
//			if ( ucStatus.StatusSysNo != AppConst.IntNull )
//				ht.Add("Status", ucStatus.StatusSysNo);
//
//			DataSet ds = ConvertDs(AdjustManager.GetInstance().GetAdjustListDs(ht));

			if ( chkAll.Checked )
			{
				Hashtable ht = new Hashtable(5);
				if ( ucProduct.ProductSysNo != AppConst.IntNull )
					ht.Add("ProductSysNo", ucProduct.ProductSysNo);
				DataSet ds = InventoryManager.GetInstance().GetInventoryDs(ht);

				base.SetDataGridPage(ds, DataGrid2);

				DataGrid2.DataSource = ds;
				DataGrid2.DataBind();
				

				int rows = 0;
				if ( Util.HasMoreRow(ds) )
					rows = ds.Tables[0].Rows.Count;
				Assert(lblMessage, rows.ToString() + " record(s)", 1);
			}
			else
			{
				Hashtable ht = new Hashtable(5);
				if ( ucStock.StockSysNo != AppConst.IntNull )
					ht.Add("StockSysNo", ucStock.StockSysNo);
				if ( ucProduct.ProductSysNo != AppConst.IntNull )
					ht.Add("ProductSysNo", ucProduct.ProductSysNo);
				if ( txtPosition1.Text.Trim() != "" )
					ht.Add("Position1", txtPosition1.Text.Trim());
				if ( txtPosition2.Text.Trim() != "" )
					ht.Add("Position2", txtPosition2.Text.Trim());

				DataSet ds = ConvertDs(InventoryManager.GetInstance().GetInventoryStockDs(ht));

				base.SetDataGridPage(ds, DataGrid1);

				DataGrid1.DataSource = ds;
				DataGrid1.DataBind();
				
				int rows = 0;
				if ( Util.HasMoreRow(ds) )
					rows = ds.Tables[0].Rows.Count;
				Assert(lblMessage, rows.ToString() + " record(s)", 1);

			}
		}

		private DataSet ConvertDs(DataSet ds)
		{
			if ( !Util.HasMoreRow(ds))
				return ds;

			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				string pos1 = Util.TrimNull(dr["position1"]);
				if ( pos1 == "" )
					pos1 = "- - -";
				string pos2 = Util.TrimNull(dr["position2"]);
				if ( pos2 == "" )
					pos2 = "- - -";

				dr["Position1"] = "<a href=\"javascript:openWindowS3('InventoryPositionSet.aspx?sysno=" + dr["sysno"].ToString() + "&opt=pos1')\">" +pos1+"</a>";
				dr["Position2"] = "<a href=\"javascript:openWindowS3('InventoryPositionSet.aspx?sysno=" + dr["sysno"].ToString() + "&opt=pos2')\">" +pos2+"</a>";
			}
			return ds;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
			this.DataGrid2.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid2_PageIndexChanged);

		}
		#endregion

		protected void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if ( chkAll.Checked )
			{
				trPosition.Visible = false;
				ucStock.StockSysNo = AppConst.IntNull;
				ucStock.Enabled = false;
				DataGrid2.Visible = true;
				DataGrid1.Visible = false;
			}
			else
			{
				trPosition.Visible = true;
				ucStock.Enabled = true;

				DataGrid2.Visible = false;
				DataGrid1.Visible = true;

			}
		}

		private void DataGrid2_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid2.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			DataGrid1.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

	}
}
